<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Package extends Common
{
    /**
     * User:Simony
     * Function: 套餐表列表
     * @return mixed
     */
    public function packageList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        //model
        $packageModel = model('common/package/Package');

        //getData
        $list = $packageModel->getList($where, 10, '*', 'sort desc');

        //assign
        $this->assign('list', $list);
        $this->assign('page', $packageModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 套餐表添加修改
     */
    public function packageAdd()
    {
        //params
        $id = input('param.id/d', '');

        //model
        $packageModel = model('common/package/Package');
        $data = $packageModel->getInfoByID($id);

        if ($this->request->isAjax()) {

            //post params
            $ajax_data = $this->request->post();

            //judge
            if ($id && !$data) ds_json_encode('err', '数据不存在');

            $res = $id ? $packageModel->edit(['id' => $id], $ajax_data) : $packageModel->add($ajax_data);
            $res ? ds_json_encode('ret', '操作成功') : ds_json_encode('err', '操作失败');
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
     * User:Simony
     * Function: 套餐购买记录列表
     * @return mixed
     */
    public function buyLogList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');
        $date_range = input('get.date_range', '');

        //where
        $where = [];

        //judge
        $field && $value && $where[] = [$field,'eq',$value];
        $search_field && $search_value && $where[] = [$search_field,'eq',$search_value];



        $name=[];
        $start_time = $end_time = '';
        if (!empty($date_range)) {
            $date_range = explode(' - ', $date_range);
            $start_time = strtotime($date_range[0]);
            $end_time = strtotime($date_range[1])+86399;
            $name['begin_date'] = $date_range[0];
            $name['end_date'] = $date_range[1];
        }
        if (!(empty($start_time)) && !(empty($end_time))) {
            $where[] = ['b_l.create_time', 'egt', $start_time];
            $where[] = ['b_l.create_time', 'elt', $end_time];
        }

        //model
        $packageBuyLogModel = model('common/package/PackageBuyLog');

        //getData
        $list = $packageBuyLogModel->getPackageBuyLogList($where, 10,'b_l.*,m.nickname,m.username,m.type as m_type');

        //assign
        $this->assign('list', $list);
        $this->assign('states',$packageBuyLogModel->states);
        $this->assign('pay_types',$packageBuyLogModel->pay_types);
        $this->assign('page', $packageBuyLogModel->page_info->render());
        $this->assign('name',$name);
        $this->assign('m_types',  model('common/member/Member')->types);
        $this->assign('types',  $packageBuyLogModel->types);
        return $this->fetch();
    }





}